﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocios;

public partial class ManterEspecificacao : System.Web.UI.Page
{
    EspecificacaoBLL aBLL = new EspecificacaoBLL();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Novo_Click(object sender, EventArgs e)
    {
        ddlTipo.Enabled = true;
        btGravar.Enabled = true;
        btCancelar.Enabled = true;
        tbLargura.Enabled = true;
        TBAltura.Enabled = true;
        TBBitrate.Enabled = true; 
        
    }

    private void cancelar()
    {
        tbCodigo.Text="";
        ddlTipo.SelectedValue = "0";
        TBBitrate.Text = "";
        tbLargura.Text = "";
        TBAltura.Text = "";
        lbErro.Text = "";

        ddlTipo.Enabled = false;
        tbLargura.Enabled = false;
        TBAltura.Enabled = false;
        TBBitrate.Enabled = false;
        btCancelar.Enabled = false;
        btGravar.Enabled = false;
        btNovo.Enabled = true;
    }


    protected void btGravar_Click(object sender, EventArgs e)
    {
        try
        {
            if(tbLargura.Text.Equals("")){
                tbLargura.Text="0";
            }
            if(TBAltura.Text.Equals("")){
                TBAltura.Text="0";
            }
            if(TBBitrate.Text.Equals("")){
                TBBitrate.Text="0";
            }
            if (tbCodigo.Text == "")
            {
                
                Especificacao a = new Especificacao(Convert.ToInt32(ddlTipo.SelectedValue), Convert.ToInt32(tbLargura.Text), Convert.ToInt32(TBAltura.Text), Convert.ToDouble(TBBitrate.Text));
                aBLL.Insere(a);
               
            }
            else
            {
                Especificacao a = new Especificacao(Convert.ToInt32(tbCodigo.Text), Convert.ToInt32(ddlTipo.SelectedValue), Convert.ToInt32(tbLargura.Text), Convert.ToInt32(TBAltura.Text), Convert.ToDouble(TBBitrate.Text));
                aBLL.Atualizar(a);
            } 
            cancelar();
                btNovo.Enabled = true;
                GridView1.DataBind();

        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }   
    protected void btCancelar_Click(object sender, EventArgs e)
    {
        cancelar();
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);
            Especificacao a = aBLL.Consultar(id);
            tbCodigo.Text = a.Codigo.ToString();
            ddlTipo.SelectedValue = a.Tipo.ToString();
            tbLargura.Text = a.Largura.ToString();
            TBAltura.Text = a.Altura.ToString();
            TBBitrate.Text = a.Bitrate.ToString();

            GridView1.DataBind();

            ddlTipo.Enabled = true;
            tbLargura.Enabled = true;
            TBAltura.Enabled = true;
            TBBitrate.Enabled = true;
            btGravar.Enabled = true;
            btCancelar.Enabled = true;
            btNovo.Enabled = false;
        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);
            Especificacao a = new Especificacao(id);
            aBLL.Excluir(a);
            GridView1.DataBind();
        }catch(Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }
}
